﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Exam.Core.DBUtility;
using System.Data;
using System.Data.OleDb;

namespace Exam.Core.DAL {

    public class Course {


        public List<Exam.Core.Model.Course> getCoursesListByMasterId(string masterid) {
            string strsql = "SELECT CourseId FROM Master WHERE MasterID="+masterid;
            object o = DbHelperOleDb.GetSingle(strsql);
            if (o == null) { o = 0; }
            string SQL = "SELECT * FROM CourseView WHERE CourseID in (" + o.ToString().Trim(',') + ") ";
            DataTable dt = DbHelperOleDb.Query(SQL).Tables[0];
            List<Exam.Core.Model.Course> list = new List<Exam.Core.Model.Course>();
            foreach (DataRow dr in dt.Rows) {
                Exam.Core.Model.Course obj = new Exam.Core.Model.Course();
                obj.Id = dr["CourseID"].ToString().Trim();
                obj.Title = dr["CourseName"].ToString().Trim();
                obj.Parent = "0";//dr["ParentId"].ToString().Trim();
                //obj.Knowledges = new Exam.Core.DAL.Knowledge().getKnowledgeList(obj.Id);
                obj.KnowCount = dr["KnowNumber"].ToString().Trim();
                obj.QuestionCount = dr["questionNumber"].ToString().Trim();
                list.Add(obj);
            }
            return list;

        }
        #region
        //public List<Exam.Core.Model.Course> getCoursesList(string courseid) {
        //    string SQL = "SELECT * FROM CourseView WHERE CourseID in (" + courseid.Trim(',') + ") ";
        //    DataTable dt = DbHelperOleDb.Query(SQL).Tables[0];
        //    List<Exam.Core.Model.Course> list = new List<Exam.Core.Model.Course>();
        //    foreach (DataRow dr in dt.Rows) {
        //        Exam.Core.Model.Course obj = new Exam.Core.Model.Course();
        //        obj.Id = dr["CourseID"].ToString().Trim();
        //        obj.Title = dr["CourseName"].ToString().Trim();
        //        obj.Parent = "0";//dr["ParentId"].ToString().Trim();
        //        //obj.Knowledges = new Exam.Core.DAL.Knowledge().getKnowledgeList(obj.Id);
        //        obj.KnowCount = dr["KnowNumber"].ToString().Trim();
        //        obj.QuestionCount = dr["questionNumber"].ToString().Trim();
        //        list.Add(obj);
        //    }
        //    return list;

        //}
        #endregion

        public Exam.Core.Model.Course getCourseById(int id) {
            string SQL = "SELECT * FROM CourseView WHERE CourseID=" + id;
            DataTable dt = DbHelperOleDb.Query(SQL).Tables[0];
            Exam.Core.Model.Course obj = new Exam.Core.Model.Course();
            obj.Id = dt.Rows[0]["CourseID"].ToString().Trim();
            obj.Title = dt.Rows[0]["CourseName"].ToString().Trim();
            obj.Parent = "0";
            obj.Knowledges = new Exam.Core.DAL.Knowledge().getKnowledgeList(obj.Id);
            obj.KnowCount = dt.Rows[0]["KnowNumber"].ToString().Trim();
            obj.QuestionCount = dt.Rows[0]["questionNumber"].ToString().Trim();
            obj.Knowledges = new Exam.Core.DAL.Knowledge().getKnowledgeList(id.ToString());//课程下知识点
            obj.QuestionTypes = new Exam.Core.DAL.QuestionType().getQuestionTypeListByCourseId(id.ToString());//该课程下题型列表
            return obj;
        }

        public bool AddCourse(string cname,string cdesc, string cidlist, string masterid) {
            string SQL = "INSERT INTO Course (CourseName,CourseDesc,ParentId) VALUES ('" + cname + "','" + cdesc + "',0)";
            int rows = DbHelperOleDb.ExecuteSql(SQL);
            bool result = false;
            if (rows > 0) {
                //将课程添加到master表中
                int id = DbHelperOleDb.GetMaxID("CourseID", "Course");
                string ids = cidlist.Trim(',') + "," + id;
                SQL = "UPDATE Master SET CourseId='" + ids.Trim() + "' WHERE MasterID=" + masterid;
                int a = DbHelperOleDb.ExecuteSql(SQL);
                if (a > 0)
                    result = true;
            }
            return result;
        }

        //删除课程
        public bool Delete(string cid) {
            bool result = false;
            string SQL = "DELETE FROM Course WHERE CourseID=" + cid;
            try {
                DbHelperOleDb.ExecuteSql(SQL);
                result = true;
            }
            catch {
                result = false;
            }
            return result;
        }

        public bool EditCourse(string cid, string cname) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE Course SET ");
            strSql.Append("CourseName=@CourseName");
            strSql.Append(" WHERE CourseID=@CourseID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@CourseName", OleDbType.VarChar),
                    new OleDbParameter("@CourseID", OleDbType.Integer,4)
                };
            parameters[0].Value = cname.Trim();
            parameters[1].Value = int.Parse(cid);
            bool result = false;
            try{
                int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
                result = true;
            }
            catch {
                result = false;
            }
            return result;
        }

        #region
        //public bool DelCourse(string cid) {
        //    string path = FilePath.CourseXmlPath.Trim();
        //    XmlDocument xmlDoc = new XmlDocument();
        //    xmlDoc.Load(path);
        //    XmlNodeList nodeList = xmlDoc.GetElementsByTagName("course");
        //    foreach (XmlNode node in nodeList) {
        //        string Id = node.Attributes["id"].Value;
        //        if (Id.Trim() == cid.Trim())
        //            node.RemoveAll();
        //    }
        //    bool result = false;
        //    try {
        //        xmlDoc.Save(path);
        //        result = true;
        //    }
        //    catch (System.Exception ex) {
        //        result = false;
        //    }
        //    finally {
        //        xmlDoc = null;
        //    }
        //    return result;
        //}


        //public bool EditCourse(string cid,string cname) {
        //    string path = FilePath.CourseXmlPath.Trim();
        //    XmlDocument xmlDoc = new XmlDocument();
        //    xmlDoc.Load(path);
        //    XmlNodeList nodeList = xmlDoc.GetElementsByTagName("course");
        //    foreach (XmlNode node in nodeList) {
        //        string Id = node.Attributes["id"].Value;
        //        if (Id.Trim() == cid.Trim())
        //            node.Attributes["id"].Value = cname;
        //    }
        //    bool result = false;
        //    try {
        //        xmlDoc.Save(path);
        //        result = true;
        //    }
        //    catch (System.Exception ex) {
        //        result = false;
        //    }
        //    finally {
        //        xmlDoc = null;
        //    }
        //    return result;
        //}
        #endregion


        internal List<Model.Course> getCourseList(string p) {
            throw new NotImplementedException();
        }
    }
}
